// import anime from 'animejs/lib/anime.es.js';
import * as THREE from "three";

import { Config } from "./Config";
import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader';
import { Card } from './Card';
import AnimeManager from './AnimeManager';

export class Cube {
  type: number = 0;
  isChoose: boolean = false;
  moveDistance: number = 0;
  moveTimer: number = 0;
  indexCol: number = 0;
  order: number = 0;
  scene: THREE.Scene | null = null;
  mesh: Card = null as any;
  anis: any[] = [];

  constructor(indexCol: number) {
    this.indexCol = indexCol;
    this.mesh = new Card()
  }

  setType(type: number): void {
    this.type = type;
    // if(!this.mesh){
    //   this.mesh = Config.card.clone() as Card;
    // }
    this.mesh.setType(type);
  }

  play(): void {
    AnimeManager.anime({
      targets: this.mesh.position,
      y: this.mesh.position.y + this.moveDistance,
      duration: this.moveTimer,
      easing: 'easeOutQuart'
    });
  }

  setPosition(p: THREE.Vector3): void {
    this.mesh.position.copy(p);
  }

  setY(y: number): void {
    this.mesh.position.y = y;
  }

  addToScene(scene: THREE.Scene): void {
    this.scene = scene;
    scene.add(this.mesh);
  }

  remove(): void {
    this.scene?.remove(this.mesh);
    console.log("remove");
  }

  updateOrder(): void {
    let num = 1;
    if (this.py > Config.BOTTOM_Y - 10 && this.py < Config.TOP_Y + 10) {
      this.order = Math.round((Config.TOP_Y - this.py) / Config.CARD_HEIGHT) + Config.CARDS_PER_COL * this.indexCol;
      // console.log("order", this.order, "TOP_Y", Config.TOP_Y, "py", this.py, "indexCol", this.indexCol)
      // num = 0.5 + 0.5 * this.order / 35
    } else {
      this.order = -1;
    }
    // this.mesh.scale.setScalar(num);
  }

  choose(v: boolean): void {
    this.mesh.run();

    this.isChoose = v;
    this.anis.forEach(a=>a.reset())
    this.anis = [];

    this.anis.push(AnimeManager.anime({
      targets: this.mesh.rotation,
      y: 3.14 * 4,
      duration: Config.CHOOSE_TIMER,
      easing: 'easeInOutQuad',
      delay: this.indexCol * 120,
    }));

    this.anis.push(AnimeManager.anime({
      targets: this.mesh.scale,
      x: 0.01,
      y: 0.01,
      z: 0.01,
      duration: Config.CHOOSE_TIMER,
      easing: 'easeInOutQuad',
      delay: this.indexCol * 120,
    }));
  }

  get py(): number {
    return this.mesh.position.y;
  }

  get isOutside(): boolean {
    return this.py < Config.BOTTOM_Y - Config.CARD_HEIGHT * 1.2 || this.py > Config.TOP_Y;
    // return this.py < Config.BOTTOM_Y || this.py > Config.TOP_Y;
  }

  get isBottom(): boolean {
    return this.py < Config.BOTTOM_Y - Config.CARD_HEIGHT * 1.2;
    // return this.py < Config.BOTTOM_Y;
  }

  
}